﻿using Mysoft.QuickCode.Web.Data;
using Mysoft.QuickCode.Web.EasyUI;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;

namespace Mysoft.QuickCode.Web
{
    /// <summary>
    /// Sql工具类
    /// </summary>
    public class CommonTool
    {

        /// <summary>
        /// 从过滤参数中解析出Sql语句过滤条件。若无，则返回null
        /// </summary>
        /// <returns></returns>
        public static ParsedFiltersResult ParseSqlFilters(List<SqlFilter> filters)
        {
            List<string> whereList = new List<string>();
            List<SqlParameter> parameters = new List<SqlParameter>();
            int i = 0;
            if (filters != null && filters.Count > 0)
            {
                foreach (var filter in filters)
                {
                    if (!string.IsNullOrEmpty(filter.value1) && !string.IsNullOrEmpty(filter.value2))
                    {
                        whereList.Add(filter.name + " >= @p" + i.ToString());
                        parameters.Add(new SqlParameter { ParameterName = "p" + i.ToString(), Value = filter.value1 ?? "" });
                        ++i;
                        whereList.Add(filter.name + " <= @p" + i.ToString());
                        parameters.Add(new SqlParameter { ParameterName = "p" + i.ToString(), Value = filter.value2 ?? "" });
                        ++i;
                    }
                    else
                    {
                        switch (filter.op)
                        {
                            case "eq":
                                whereList.Add(filter.name + " = @p" + i.ToString());
                                parameters.Add(new SqlParameter { ParameterName = "p" + i.ToString(), Value = filter.value1 ?? "" });
                                break;
                            case "lt":
                                whereList.Add(filter.name + " < @p" + i.ToString());
                                parameters.Add(new SqlParameter { ParameterName = "p" + i.ToString(), Value = filter.value1 ?? "" });
                                break;
                            case "gt":
                                whereList.Add(filter.name + " > @p" + i.ToString());
                                parameters.Add(new SqlParameter { ParameterName = "p" + i.ToString(), Value = filter.value1 ?? "" });
                                break;
                            default:
                                whereList.Add(filter.name + " like @p" + i.ToString());
                                parameters.Add(new SqlParameter { ParameterName = "p" + i.ToString(), Value = "%" + (filter.value1 ?? "") + "%" });
                                break;
                        }
                        ++i;
                    }
                }
                if (filters.Count > 0)
                {
                    return new ParsedFiltersResult()
                    {
                        SqlText = string.Join(" and ", whereList.ToArray()),
                        Parameters = parameters
                    };
                }
            }
            return null;
        }

        /// <summary>
        /// 读取文件内容
        /// </summary>
        /// <param name="path">文件路径，用 . 分隔。如 List.sql 路径为 PayPlan.YWCS.List。</param>
        /// <returns></returns>
        public static string GetSqlContent(string path)
        {
            path = path.Replace(@".", @"\");
            return System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("/") + path + ".sql");
        }
    }
}
